﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;

namespace ddcCajamarca.Web.Account
{
    public partial class frmListaRoles : System.Web.UI.Page
    {
        public bool puedeEditar;

        protected void Page_Load(object sender, EventArgs e)
        {
            puedeEditar = User.IsInRole("Admin") ? true : false;

            var accion = Request.QueryString["accion"];
            var id = Request.QueryString["rol"];

            if (accion != null && accion == "delete" && id != "Admin")
            {
                try
                {
                    Roles.DeleteRole(id);
                }
                catch (Exception)
                {
                    lbError.Text = "No se puede eliminar el rol *** " + id + " ***, porque contiene usuarios";
                }
            }

            gvRoles.DataSource = Roles.GetAllRoles();
            gvRoles.DataBind();
        }

        protected bool verLink()
        {
            var rpt = false;

            if (User.IsInRole("Admin"))
            {
                rpt = true;
            }

            return rpt;
        }

        protected void btnCrear_Click(object sender, EventArgs e)
        {
            if (!Roles.RoleExists(txtNuevoRol.Text))
            {
                Roles.CreateRole(txtNuevoRol.Text);
                Response.Redirect("frmListaRoles.aspx");
            }
            else
            {
                lbError.Text = "El rol *** " + txtNuevoRol.Text + " *** ya existe";
            }
        }
    }
}